package com.taotao.rest.service.impl;

import com.taotao.mapper.ItemCategoryMapper;
import com.taotao.pojo.ItemCategory;
import com.taotao.rest.pojo.ItemCat;
import com.taotao.rest.service.ItemCategoryService;
import com.taotao.rest.util.ItemCategoryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hong.zheng
 * @Description
 * @date 2018-09-07-10:17
 **/
@Service
public class ItemCategoryServiceImpl implements ItemCategoryService {

    @Autowired
    private ItemCategoryMapper itemCategoryMapper;

    @Override
    public ItemCategoryResult queryAllCategory() throws Exception {

        ItemCategoryResult itemCategoryResult = new ItemCategoryResult();
        itemCategoryResult.setData(getItemCategoryList(0l));

        return itemCategoryResult;
    }

    private List<?> getItemCategoryList(long parentid) {
        List<ItemCategory> list = itemCategoryMapper.getItemCategoryListByParentId(parentid);
        List dataList = new ArrayList();
        for (ItemCategory itemCategory : list) {
            //判断是否为父节点
            if (itemCategory.getParent()) {
                ItemCat itemCat = new ItemCat();
                itemCat.setUrl("/category/" + itemCategory.getId() + ".html");
                itemCat.setName(itemCategory.getName());
                //递归调用
                itemCat.setItem(getItemCategoryList(itemCategory.getId()));
                //添加到列表
                dataList.add(itemCat);
            } else {
                String catItem = "/item/" + itemCategory.getId() + ".html|" + itemCategory.getName();
                dataList.add(catItem);
            }
        }
        return dataList;
    }

}
